package com.two.lbms.utils;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.connector.Response;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/7/30 19:57
 */

public class LoginCaptchaUtil {

    private static String captchaCode;

    /**
     * 生成图形验证码
     * @param response 响应
     */
    public static void generateCaptcha(HttpServletResponse response) {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(90,30,4,10);
        circleCaptcha.createCode();
        ServletOutputStream outputStream = null;
        // lineCaptcha.write("E:\\yjx_code\\lbms\\lbms\\src\\main\\resources\\static\\images\\captcha.jpg");
        try {
            outputStream = response.getOutputStream();
            circleCaptcha.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        captchaCode = circleCaptcha.getCode();
        System.out.println(circleCaptcha.getCode());
    }

    /**
     * 获取图片验证码
     * @return 图片验证码
     */
    public static String getCaptchaCode() {
        return captchaCode;
    }


}
